1 using System;
2 using
System.Collections.Generic;
3 using
System.ComponentModel;
4 using
System.Data;
5 using
System.Drawing;
6 using
System.Text;
7 using
System.Windows.Forms;
8
9
10 namespace
VietGraph
11 {
12     
public partial class Graph : Form
13     {
14         
public Graph()
15         {
16             InitializeComponent();
17         }
18
19         
#region Public Methods
20         
public void AddExpression(string text, Color color)
21         {
22             expPlotter.AddExpression((IEvaluatable)
new Expression(text), color, true);
23             
//lstExpressions.Items.Add(text);
24             
//lstExpressions.SetItemChecked(lstExpressions.Items.IndexOf(text), true);
25         }
26         
public void RemoveAllExpressions()
27         {
28             expPlotter.RemoveAllExpressions();
29             
//lstExpressions.Items.Clear();
30         }
31         
public void SetRange(double startX, double endX, double startY, double endY)
32         {
33             expPlotter.SetRangeX(startX, endX);
34             expPlotter.SetRangeY(startY, endY);
35         }
36         
public void SetDivisions(int divX, int divY)
37         {
38             expPlotter.DivisionsX = divX;
39             expPlotter.DivisionsY = divY;
40         }
41         
public void SetMode(GraphMode mode, int sensitivity)
42         {
43             expPlotter.GraphMode = mode;
44             
if (mode == GraphMode.Polar)
45             {
46                 expPlotter.PolarSensitivity = sensitivity;
47                 
this.lblSensitivity.Text = "Polar sensitivity: " + expPlotter.PolarSensitivity;
48             }
49         }
50         
public void SetPenWidth(int width)
51         {
52             expPlotter.PenWidth = width;
53         }
54
55         
#endregion
56
57         
#region Private Methods
58
59         
private double GetR(double X, double Y)
60         {
61             
return Math.Sqrt(X * X + Y * Y);
62         }
63         
private double GetTheta(double X, double Y)
64         {
65             
double dTheta;
66             
if (X == 0)
67             {
68                 
if (Y > 0)
69                     dTheta = Math.PI /
2;
70                 
else
71                     dTheta = -Math.PI /
2;
72             }
73             
else
74                 dTheta = Math.Atan(Y / X);
75
76             
//actual range of theta is from 0 to 2PI
77             
if (X < 0)
78                 dTheta = dTheta + Math.PI;
79             
else if (Y < 0)
80                 dTheta = dTheta +
2 * Math.PI;
81             
return dTheta;
82         }
83
84         
private System.Drawing.Imaging.ImageFormat GetImageFormat(string filename)
85         {
86             
string[] tempArray = filename.Split('.');
87             
string extension = tempArray[tempArray.Length - 1];
88             
switch (extension)
89             {
90                 
case "bmp":
91                     
return System.Drawing.Imaging.ImageFormat.Bmp;
92                 
case "jpg":
93                     
return System.Drawing.Imaging.ImageFormat.Jpeg;
94                 
case "gif":
95                     
return System.Drawing.Imaging.ImageFormat.Gif;
96                 
case "png":
97                     
return System.Drawing.Imaging.ImageFormat.Png;
98                 
case "tiff":
99                     
return System.Drawing.Imaging.ImageFormat.Tiff;
100                 
case "wmf":
101                     
return System.Drawing.Imaging.ImageFormat.Wmf;
102                 
default:
103                     
return System.Drawing.Imaging.ImageFormat.Bmp;
104             }
105
106         }
107
108         
#endregion
109
110         
#region Event Handlers
111
112         
private void Graph_Load(object sender, EventArgs e)
113         {
114             expPlotter.MouseMove +=
new MouseEventHandler(ExpPlotter_OnMouseMove);
115             
//expPlotter.MouseWheel += new MouseEventHandler(ExpPlotter_OnMouseWheel);
116             
this.lblSensitivity.Text = "";
117             
if (ExpressionHelper.Cartesian == true)
118             {
119                 
this.cartesianToolStripMenuItem.Checked = true;
120                 
this.polarToolStripMenuItem.Checked = false;
121             }
122             
else
123             {
124                 
this.polarToolStripMenuItem.Checked = true;
125                 
this.cartesianToolStripMenuItem.Checked = false;
126             }
127         }
128
129         
//private void ExpPlotter_OnMouseWheel(object sender, MouseEventArgs e)
130         
//{
131         
// if (e.Delta > 0)
132         
// expPlotter.ZoomIn();
133         
// else if (e.Delta < 0)
134         
// expPlotter.ZoomOut();
135         
// expPlotter.Refresh();
136         
//}
137
138         
private void ExpPlotter_OnMouseMove(object sender, MouseEventArgs e)
139         {
140             
double currentX, currentY;
141             currentX = (e.X - expPlotter.Width /
2) * expPlotter.ScaleX / expPlotter.Width * 2.25 + expPlotter.ForwardX;
142             currentY = (expPlotter.Width /
2 - e.Y) * expPlotter.ScaleY / expPlotter.Width * 2.25 + expPlotter.ForwardY;
143             
if (expPlotter.GraphMode == GraphMode.Polar)
144             {
145                 
double r = GetR(currentX, currentY);
146                 
double theta = GetTheta(currentX, currentY);
147                 currentX = r;
148                 currentY = theta;
149             }
150             currentX = Math.Round(currentX,
3);
151             currentY = Math.Round(currentY,
3);
152             
this.lblPosition.Text = "Vị trí hiện tại của chuột: " + currentX + ", " + currentY;
153         }
154
155
156         
private void btnShowOrigin_Click(object sender, EventArgs e)
157         {
158             expPlotter.ForwardX = expPlotter.ForwardY =
0;
159             expPlotter.Refresh();
160         }
161
162         
private void btnDefault_Click(object sender, EventArgs e)
163         {
164             expPlotter.RestoreDefaults();
165             expPlotter.Refresh();
166         }
167
168
169         
private void btnText_Click(object sender, EventArgs e)
170         {
171             expPlotter.DisplayText = !expPlotter.DisplayText;
172             expPlotter.Refresh();
173         }
174
175         
private void Graph_Resize(object sender, EventArgs e)
176         {
177             expPlotter.Refresh();
178         }
179
180
181         
private void expPlotter_Click(object sender, EventArgs e)
182         {
183             expPlotter.Select();
184         }
185
186         
#endregion
187
188         
private void lstExpressions_SelectedIndexChanged(object sender, EventArgs e)
189         {
190
191         }
192
193         
private void thoátToolStripMenuItem1_Click(object sender, EventArgs e)
194         {
195             
this.Close();
196         }
197
198         
private void cartesianToolStripMenuItem_Click(object sender, EventArgs e)
199         {
200             
this.cartesianToolStripMenuItem.Checked = true;
201             
this.polarToolStripMenuItem.Checked = false;
202             
if (expPlotter.GraphMode != GraphMode.Rectangular)
203             {
204                 
this.lblSensitivity.Text = "";
205                 expPlotter.GraphMode = GraphMode.Rectangular;
206                 expPlotter.Refresh();
207             }
208         }
209
210         
private void polarToolStripMenuItem_Click(object sender, EventArgs e)
211         {
212             
this.cartesianToolStripMenuItem.Checked = false;
213             
this.polarToolStripMenuItem.Checked = true;
214             
if (expPlotter.GraphMode != GraphMode.Polar)
215             {
216                 
this.lblSensitivity.Text = "Độ nhạy polar: " + expPlotter.PolarSensitivity;
217                 expPlotter.GraphMode = GraphMode.Polar;
218                 expPlotter.Refresh();
219             }
220         }
221
222         
private void ẩnHiệnKhungLướiToolStripMenuItem_Click(object sender, EventArgs e)
223         {
224             expPlotter.ToggleGrids();
225             expPlotter.Refresh();
226         }
227
228         
private void ẩnHiệnBiểuThứcToolStripMenuItem_Click(object sender, EventArgs e)
229         {
230             expPlotter.DisplayText = !expPlotter.DisplayText;
231             expPlotter.Refresh();
232         }
233
234         
private void btnLuuDoThi_Click(object sender, EventArgs e)
235         {
236             SaveFileDialog dialog =
new SaveFileDialog();
237             dialog.Title =
"Luu do thi";
238             
string filter = "Bitmap (*.bmp)|*.bmp|" +
239                             
"JPEG (*.jpg)|*.jpg|" +
240                             
"GIF (*.gif)|*.gif|" +
241                             
"PNG (*.png)|*.png|" +
242                             
"TIFF (*.tiff)|*.tiff|" +
243                             
"WMF (*.wmf)|*.wmf";
244             dialog.Filter = filter;
245             dialog.FileName =
"do thi";
246             
if (dialog.ShowDialog() == DialogResult.OK)
247             {
248                 Bitmap bmp = expPlotter.GetGraphBitmap();
249                 bmp.Save(dialog.FileName, GetImageFormat(dialog.FileName));
250                 MessageBox.Show(
"Đồ thị đã được lưu thành công tại địa chỉ " + dialog.FileName, "Đã lưu");
251             }
252         }
253
254         
private void btnSaoChepDoThi_Click(object sender, EventArgs e)
255         {
256             expPlotter.CopyToClipboard();
257         }
258
259         
private void btnPhongTo_Click(object sender, EventArgs e)
260         {
261             expPlotter.ZoomIn();
262             expPlotter.Refresh();
263         }
264
265         
private void btnThuNho_Click(object sender, EventArgs e)
266         {
267             expPlotter.ZoomOut();
268             expPlotter.Refresh();
269         }
270
271         
private void btnPhongToTrucX_Click(object sender, EventArgs e)
272         {
273             expPlotter.ZoomInX();
274             expPlotter.Refresh();
275         }
276
277         
private void btnThuNhoTrucX_Click(object sender, EventArgs e)
278         {
279             expPlotter.ZoomOutX();
280             expPlotter.Refresh();
281         }
282
283         
private void btnPhongToTrucY_Click(object sender, EventArgs e)
284         {
285             expPlotter.ZoomInY();
286             expPlotter.Refresh();
287         }
288
289         
private void btnThuNhoTrucY_Click(object sender, EventArgs e)
290         {
291             expPlotter.ZoomOutY();
292             expPlotter.Refresh();
293         }
294
295         
private void btnLenTren_Click(object sender, EventArgs e)
296         {
297             expPlotter.MoveUp(
1);
298             expPlotter.Refresh();
299         }
300
301         
private void btnXuongDuoi_Click(object sender, EventArgs e)
302         {
303             expPlotter.MoveDown(
1);
304             expPlotter.Refresh();
305         }
306
307         
private void btnSangPhai_Click(object sender, EventArgs e)
308         {
309             expPlotter.MoveRight(
1);
310             expPlotter.Refresh();
311         }
312
313         
private void btnSangTrai_Click(object sender, EventArgs e)
314         {
315             expPlotter.MoveLeft(
1);
316             expPlotter.Refresh();
317         }
318
319         
private void btnTangDoNhayPolar_Click(object sender, EventArgs e)
320         {
321             
if (expPlotter.GraphMode == GraphMode.Polar && expPlotter.PolarSensitivity < 500)
322             {
323                 expPlotter.PolarSensitivity +=
50;
324                 expPlotter.Refresh();
325                 
this.lblSensitivity.Text = "Độ nhạy polar: " + expPlotter.PolarSensitivity;
326             }
327         }
328
329         
private void btnGiamDoNhayPolar_Click(object sender, EventArgs e)
330         {
331             
if (expPlotter.GraphMode == GraphMode.Polar && expPlotter.PolarSensitivity > 50)
332             {
333                 expPlotter.PolarSensitivity -=
50;
334                 expPlotter.Refresh();
335                 
this.lblSensitivity.Text = "Độ nhạy polar: " + expPlotter.PolarSensitivity;
336             }
337         }
338
339
340
341         
private void btnLuuDoThi_MouseMove(object sender, MouseEventArgs e)
342         {
343             
this.lblChuThich.Text = "Lưu đồ thị";
344         }
345
346         
private void btnSaoChepDoThi_MouseMove(object sender, MouseEventArgs e)
347         {
348             
this.lblChuThich.Text = "Sao chép tới clipboard";
349         }
350
351         
private void btnPhongTo_MouseMove(object sender, MouseEventArgs e)
352         {
353             
this.lblChuThich.Text = "Phóng to";
354         }
355
356         
private void btnThuNho_MouseMove(object sender, MouseEventArgs e)
357         {
358             
this.lblChuThich.Text = "Thu nhỏ";
359         }
360
361         
private void btnPhongToTrucX_MouseMove(object sender, MouseEventArgs e)
362         {
363             
this.lblChuThich.Text = "Phóng to trục X";
364         }
365
366         
private void btnThuNhoTrucX_MouseMove(object sender, MouseEventArgs e)
367         {
368             
this.lblChuThich.Text = "Thu nhỏ trục X";
369         }
370
371         
private void btnPhongToTrucY_MouseMove(object sender, MouseEventArgs e)
372         {
373             
this.lblChuThich.Text = "Phóng to trục Y";
374         }
375
376         
private void btnThuNhoTrucY_MouseMove(object sender, MouseEventArgs e)
377         {
378             
this.lblChuThich.Text = "Thu nhỏ trục Y";
379         }
380
381         
private void btnTangDoNhayPolar_MouseMove(object sender, MouseEventArgs e)
382         {
383             
this.lblChuThich.Text = "Tăng độ nhạy polar";
384         }
385
386         
private void btnGiamDoNhayPolar_MouseMove(object sender, MouseEventArgs e)
387         {
388             
this.lblChuThich.Text = "Giảm độ nhạy polar";
389         }
390
391         
private void btnLenTren_MouseMove(object sender, MouseEventArgs e)
392         {
393             
this.lblChuThich.Text = "Lên trên";
394         }
395
396         
private void btnXuongDuoi_MouseMove(object sender, MouseEventArgs e)
397         {
398             
this.lblChuThich.Text = "Xuống dưới";
399         }
400
401         
private void btnSangPhai_MouseMove(object sender, MouseEventArgs e)
402         {
403             
this.lblChuThich.Text = "Sang phải";
404         }
405
406         
private void btnSangTrai_MouseMove(object sender, MouseEventArgs e)
407         {
408             
this.lblChuThich.Text = "Sang trái";
409         }
410
411         
private void expPlotter_MouseMove(object sender, MouseEventArgs e)
412         {
413             
this.lblChuThich.Text = "Hãy kích chuột phải để thoát";
414         }
415
416         
private void mniThemBotBieuThuc_Click(object sender, EventArgs e)
417         {
418             ThemBotBieuThuc frm =
new ThemBotBieuThuc();
419             frm.ShowDialog();
420         }
421
422         
private void mniThongTinDoThi_Click(object sender, EventArgs e)
423         {
424             ThongTinDoThi frm =
new ThongTinDoThi();
425             frm.ShowDialog();
426         }
427         
428     }
429 }



Phần mềm vẽ đồ thị C# 6.572 lượt xem

Gõ tìm kiếm nhanh...